import request from '@/common/request'

export default {
  
  getList(map = {}) {
    map.storage = map.storage || '';
    return new Promise((resolve, reject) => {
      request.get('/image', map).then(response => {
        resolve(response)
      })
    })
  },
  
  delete(id) {
    return new Promise((resolve, reject) => {
      request.delete('/image/' + id).then(response => {
        resolve(response)
      })
    })
  },
  
  
  //直接传七牛云
  async upload(data) {
    const response = await request.get("/config/storage");
    const storageType = response.data.name;
    console.log(storageType);
    switch (storageType) {
      case "local": {
        return this.uploadLocal(data);
      }
      case "qiniu": {
        return this.uploadQiniu(data);
      }
    }
  },
  
  uploadLocal(data) {
    // console.log(data);
    return new Promise((resolve, reject) => {
      data.append("storage", "local");
      request
      .post("/image", data, {
        headers: {
          "Content-Type": "multipart/form-data"
        }
      })
      .then(response => {
        resolve(response);
      })
      .catch(error => {
        reject(error);
      });
    });
  },
  
  //直接传七牛云
  uploadQiniu(data) {
    
    return new Promise((resolve, reject) => {
      request.post("/qiniu").then(response => {
        const token = response.data.token;
        const domain = response.data.domain;
        // console.log(token);
        
        data.append("token", token);
        //七牛云单一文件上传必须使用token,file作为key
        //const fly = new Fly();
        request.post("http://up.qiniu.com",data,{
          baseURL: '', //覆盖实现
          headers: {
            "Content-Type": "multipart/form-data"
          }
        })
        .then(function (res) {
          console.log("res", res);
         
          let postData = new FormData();
          postData.append("storage", "qiniu");
          postData.append("base_url", domain);
          postData.append("path", '/' + res.key);
          
          request
          .post("/image", postData, {
            headers: {
              "Content-Type": "multipart/form-data"
            }
          })
          .then(response => {
            resolve({
              errno: 0,
              errmsg: "",
              data: {
                base_url: domain,
                path: '/' + res.key
              }
            });
          })
          .catch(error => {
            reject(error);
          });
          
          
          // resolve({
          //   errno: 0,
          //   errmsg: "",
          //   data: {
          //     base_url: domain,
          //     path: '/' + res.data.key
          //   }
          // });
        })
        .catch(function (err) {
          console.log("err", err);
          reject(err);
        });
      });
    });
  }
};
